﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Configuration;

using System.IO;
using System.Threading;


namespace Clarendon.AzureBilling.Library
{
    /// <summary>
    /// The ServiceManager class sends a request to the management endpoint using the REST Management API in Azure
    /// </summary>
    public class ServiceManager
    {
        /// <summary>
        /// Used to hold a mutex to signal a blocking call 
        /// </summary>
        internal readonly Semaphore Semaphore = null;
        /// <summary>
        /// A delegate response parser 
        /// </summary>
        public delegate void AsyncResponseParser(HttpWebResponse response);
        /// <summary>
        /// Used to propogate an exception thrown by Azure
        /// </summary>
        public delegate void AsyncResponseException(WebException exception);

        /// <summary>
        /// Default constructor creates a fire and forget async query manager 
        /// </summary>
        public ServiceManager()
        {
        }

        /// <summary>
        /// Overloaded constructor used to aid releasing a blocking call to the Service Manager
        /// </summary>
        public ServiceManager(Semaphore semaphore)
            : this()
        {
            Semaphore = semaphore;
        }

        #region Properties 

        /// <summary>
        /// This is the filepath for the certificate file
        /// </summary>
        public string CertificateFile
        {
            get
            {
                return ConfigurationManager.AppSettings["CertificateFile"];
            }
        }

        /// <summary>
        /// This is the base uri for the management request
        /// </summary>
        public string ManagementBaseUri
        {
            get
            {
                return ConfigurationManager.AppSettings["ManagementApiBaseUri"];
            }
        }

        /// <summary>
        /// This is the subscription id
        /// </summary>
        public string SubscriptionId
        {
            get
            {
                return ConfigurationManager.AppSettings["SubscriptionId"];
            }
        }

        /// <summary>
        /// This is the hosted service request type e.g.
        /// </summary>
        public string HostedServiceRequest 
        { 
            get
            {
                return ConfigurationManager.AppSettings["HostedServiceRequest"];
            }

        }

        /// <summary>
        /// Returns the default Xml schema for windows azure
        /// </summary>
        public static string DefaultWindowsAzureXmlNamespace 
        { 
            get
            {
                return ConfigurationManager.AppSettings["DefaultWindowsAzureXmlSchema"];
            }
        }

        /// <summary>
        /// This is a list of hosted services
        /// </summary>
        public List<HostedService> HostedServices { get; set; }

        #endregion
    }
}
